package cn.weibo.repository.hibernate;

import cn.weibo.entity.UserEntity;
import cn.weibo.repository.UserRepository;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("userOfHibernateRepository")
public class UserOfHibernateRepository extends BaseHibernateRepository implements UserRepository {

    private static final Logger logger = LoggerFactory.getLogger(UserOfHibernateRepository.class);

    @Override
    public void insertUser(UserEntity userEntity) {
        this.getCurrentSession().save(userEntity);
    }

    @Override
    public UserEntity getUserByAccount(String account) {
        Criteria criteria = this.getCurrentSession().createCriteria(UserEntity.class);
        Object result = criteria.add(Restrictions.eq("account", account)).uniqueResult();
        return (UserEntity) result;
    }

    @Override
    public long getUsersCount() {
        Criteria criteria = this.getCurrentSession().createCriteria(UserEntity.class);
        List list = criteria.list();
        return list == null ? 0 : list.size();
    }
}
